-
-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added valid_password method in ValidationRules #227
Added valid_password method in ValidationRules #227
Conversation
I think that we should separate the code responsible for validating password to a separate method in the Then we should use this method in validation rule since in the future Also... if this rule is intended only for currently logged in users, why not skip the parameter part and use |
5e9e496
to
ceccea0
Compare
@michalsn You're right about not needing parameter so I've updated the code to get the user via auth helper user() method. I've also added one additional rule valid_password_with which works in the same manner as required_with so I had this rules previously. Which also was impacted by a CI4 issue codeigniter4/CodeIgniter4#2953
Which can now be replaced with single rule
I'like the idea of using LocalAuthenticator but splitting the password check will also require updating the AuthenticatorInterface. So it might be a tricky to do this properly. The only way that looks feasible to me is moving password check to method called validate_password($entity, $password) And then use is_callable on Authenticator to check if validate_password exists as it does not make sense to have validate_password in the interface as it won't be possible to implement it in any password less authentication. |
My personal preference is that if something can be done with simple 2 rules, then adding another one to do the same is unnecessary. Because we simply end up with an additional code that we have to maintain.
Actually I can imagine Maybe Lonnie will have some opinion about it and idea how to handle this. Or maybe he will say to just leave it as it is :) |
Sorry this one slipped by me. I agree with @michalsn here. I'm good adding that to the interface, but don't think |
@najdanovicivan Are you interested in making these changes or can I close? |
@lonnieezell I’m on it |
8e0ae51
to
cd13444
Compare
I've done the changes to include validate_password method in the AuthenticatorInterface but now I have some additional thoughts What about renaming the method to validate_credential($user, $credential) that way it will be more generic so it will be useable for example with session tokens Now in valid password $authenticator is get with Service methods which have $authenticationLib set to It might be a good idea to change rule name to valid_credential as well and add an optional parameter for the authenticationLib with default value of |
cd13444
to
965b233
Compare
@lonnieezell @MGatner Can we see this one through so it can get merged. I did not get any response related to renaming the method to validate credential |
…nd ValidationRules
965b233
to
e1cb85c
Compare
@lonnieezell will have to weigh in on the name change. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking this over more during my review and I think this is a little too limited of a use-case to be included in the core library.
Thanks for your work on it, though, and hopefully you'll consider making other PR's in the future.
public function validate_password(User $user, string $password) : bool | ||
{ | ||
// Can't validate without a password. | ||
if (empty($credentials['password']) || count($credentials) < 2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a few problems here:
- The
$credentials
array doesn't exist in this method - I prefer to return early instead of wrapping code in an if statement like this.
- Even if all of the other stuff was good, the current implementation doesn't always return a boolean like the method signature states.
@@ -60,6 +60,27 @@ public function strong_password(string $value, string &$error1 = null, array $da | |||
return $result; | |||
} | |||
|
|||
/** | |||
* A validation helper method to check if the passed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* A validation helper method to check if the passed | |
* A validation helper method to check if the |
* | ||
* @return bool | ||
*/ | ||
public function validate_password(User $user, string $password) : bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically all method names are camel-cased unless used for validation rules.
public function validate_password(User $user, string $password) : bool | |
public function validatePassword(User $user, string $password) : bool |
Added method in validation rules to be able to check user current password. This is intended to be used in admin form for checks when user is modifying password.
The rule takes one parameter which is user ID so it can be used in form/model validation like this
valid_password[{id}]